package com.sky.business.sys.controller;

import com.sky.business.sys.domain.CameraConfig;
import com.sky.business.sys.domain.CameraPreset;
import com.sky.business.sys.param.DNChangeParam;
import com.sky.business.sys.param.OnvifMoveParam;
import com.sky.business.sys.param.OnvifParam;
import com.sky.business.sys.param.OnvifPresetParam;
import com.sky.business.sys.service.CameraConfigService;
import com.sky.business.sys.service.OnvifService;
import com.sky.common.api.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.xml.soap.SOAPException;
import java.net.ConnectException;
import java.util.List;

@Tag(name = "Onvif控制管理", description = "Onvif控制层")
@RequestMapping("/onvif")
@RestController
@Validated
public class OnvifController {

    @Autowired
    private OnvifService onvifService;

    @Autowired
    private CameraConfigService cameraConfigService;

    /**
     * 开始移动
     * @param onvifMoveParam
     * @return
     * @throws SOAPException
     * @throws ConnectException
     */
    @Operation(summary = "移动")
    @PostMapping(value = "/move", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<Object> move(@RequestBody OnvifMoveParam onvifMoveParam) throws SOAPException, ConnectException {
        int verticalFlag = 1;
        if(onvifMoveParam.getTilt()!=0){
            CameraConfig cameraConfig = cameraConfigService.findByIpAddrAndPort(onvifMoveParam.getIp(),onvifMoveParam.getPort());
            if(cameraConfig!=null && cameraConfig.getVerticalFlip()!=null && cameraConfig.getVerticalFlip()){
                verticalFlag = -1;
            }
        }
        onvifService.move(onvifMoveParam.getIp(),onvifMoveParam.getPort(), onvifMoveParam.getUsername(),
                onvifMoveParam.getPassword(), (onvifMoveParam.getPan()* onvifMoveParam.getSpeed()),
                (verticalFlag*onvifMoveParam.getTilt()* onvifMoveParam.getSpeed()), (onvifMoveParam.getZoom()* onvifMoveParam.getSpeed()));
        return Result.success();
    }

    /**
     * 停止移动
     * @param onvifParam
     * @return
     * @throws SOAPException
     * @throws ConnectException
     */
    @Operation(summary = "停止")
    @PostMapping(value = "/stop", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<Object> stop(@RequestBody OnvifParam onvifParam) throws SOAPException, ConnectException {
        onvifService.stop(onvifParam.getIp(), onvifParam.getPort(),onvifParam.getUsername(), onvifParam.getPassword());
        return Result.success();
    }

    /**
     * 保存当前位置为预置点
     * @param onvifPresetParam
     * @return
     * @throws SOAPException
     * @throws ConnectException
     */
    @Operation(summary = "保存预置点")
    @PostMapping(value = "/insertPreset", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<Object> insertPreset(@RequestBody OnvifPresetParam onvifPresetParam) throws SOAPException, ConnectException {
        onvifService.setPreset(onvifPresetParam.getCameraConfigId(), onvifPresetParam.getIp(),onvifPresetParam.getPort(),
                onvifPresetParam.getUsername(), onvifPresetParam.getPassword());
        return Result.success();
    }

    /**
     * 设备的预置点列表
     * @param id
     * @return
     */
    @Operation(summary = "预置点列表")
    @GetMapping(value = "/presetData", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<List<CameraPreset>> presetData(Integer id){
        List<CameraPreset> presetList = onvifService.getPresetData(id);
        return Result.success(presetList,"请求成功");
    }

    /**
     * 跳转预置点
     * @param onvifPresetParam
     * @return
     * @throws SOAPException
     * @throws ConnectException
     */
    @Operation(summary = "跳转到预置点")
    @PostMapping(value = "/gotoPreset", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<Object> gotoPreset(@RequestBody OnvifPresetParam onvifPresetParam) throws SOAPException, ConnectException {
        onvifService.gotoPreset(onvifPresetParam.getIp(), onvifPresetParam.getPort(),  onvifPresetParam.getUsername(),
                onvifPresetParam.getPassword(), onvifPresetParam.getPresetToken());
        return Result.success();
    }

    /**
     * 删除预置点
     * @param onvifPresetParam
     * @return
     * @throws SOAPException
     * @throws ConnectException
     */
    @Operation(summary = "删除预置点")
    @PostMapping(value = "/delPreset", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<Object> delPreset(@RequestBody OnvifPresetParam onvifPresetParam) throws SOAPException, ConnectException {
        onvifService.delPreset(onvifPresetParam.getPresetId(), onvifPresetParam.getIp(), onvifPresetParam.getPort(),onvifPresetParam.getUsername(),
                onvifPresetParam.getPassword(), onvifPresetParam.getPresetToken());
        return Result.success();
    }

    /**
     * 更改预置点名称
     * @param
     * @return
     */
    @Operation(summary = "更改预置点名称")
    @PostMapping(value = "/updatePresetName", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<Object> updatePresetName(@RequestBody OnvifPresetParam onvifPresetParam) throws SOAPException, ConnectException {
        onvifService.updatePresetName(onvifPresetParam.getPresetId(), onvifPresetParam.getIp(),onvifPresetParam.getPort(), onvifPresetParam.getUsername(), onvifPresetParam.getPassword(), onvifPresetParam.getName(), onvifPresetParam.getPresetToken());
        return Result.success();
    }

    /**
     * 设置日夜模式切换
     * @param dnChangeParam
     * @return
     * @throws SOAPException
     * @throws ConnectException
     */
    @PostMapping(value = "/dnChange", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<Object> dnChange(@RequestBody DNChangeParam dnChangeParam) throws SOAPException, ConnectException {
        onvifService.setIrCutFilterMode(dnChangeParam.getIp(), dnChangeParam.getPort(), dnChangeParam.getUsername(), dnChangeParam.getPassword(), dnChangeParam.getMode());
        return Result.success();
    }

}
